package com.example.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.Value;
import org.apache.commons.lang3.time.DateUtils;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 生成token
 * @author: uu
 * @data: 2023/7/18 下午 02:32
 * @version: v1.0
 */

public class JwtUtil {

    private String key;

    public JwtUtil(String key) {
        this.key = key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    //创建jwt
    public String createJwt(Map<String, Object> data, Integer minute) throws Exception{
        Date curDate = new Date();
        SecretKey secretKey = Keys.hmacShaKeyFor(key.getBytes(StandardCharsets.UTF_8));
        String jwt = Jwts.builder().signWith(secretKey, SignatureAlgorithm.HS256)
                                    .setExpiration(DateUtils.addMinutes(curDate,minute))
                                    .setIssuedAt(curDate)
                                    .setId(UUID.randomUUID().toString().replace("-","").toUpperCase())
                                    .addClaims(data)
                                    .compact();
        return jwt;
    }

    // 读取jwt
    public Claims readJwt(String jwt) throws Exception{
        SecretKey secretKey = Keys.hmacShaKeyFor(key.getBytes(StandardCharsets.UTF_8));
        Claims body = Jwts.parserBuilder()
                .setSigningKey(secretKey)
                .build().parseClaimsJws(jwt).getBody();
        return body;
    }
}
